package com.zhengjiang.purchase.controller;

import com.zhengjiang.purchase.model.vo.Result;
import com.zhengjiang.purchase.service.PurchaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author zhengjiang
 * @version 1.0.0
 * @description 高并发抢购案例
 * @date 2021/5/22
 **/
@RestController
public class PurchaseController {

    @Autowired
    private PurchaseService purchaseService;

    @GetMapping("/test")
    public ModelAndView goTestPage(){
        return new ModelAndView("test");
    }

    @PostMapping("/purchase")
    public Result purchase(Long userId,Long productId,Integer quantity){
        boolean success = purchaseService.purchase(userId, productId, quantity);
        String message = success ? "抢购成功" : "抢购失败";
        return Result.builder().success(success).message(message).build();
    }


}
